package com.location.footprint.activity;

import android.content.res.ColorStateList;
import android.os.Build;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.location.footprint.R;
import com.location.footprint.base.BaseAppAplication;
import com.location.footprint.eventbus.EventCode;
import com.location.footprint.eventbus.EventMessage;
import com.location.footprint.fragment.FootPrintFragment;
import com.location.footprint.fragment.MapFragment;
import com.location.footprint.fragment.UserFragment;
import com.location.footprint.toolbar.QMUIStatusBarHelper;
import com.location.footprint.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {


    @BindView(R.id.img_footprint)
    ImageView imgFootprint;
    @BindView(R.id.img_city)
    ImageView imgCity;
    @BindView(R.id.img_user)
    ImageView imgUser;
    private List<Fragment> fragments = new ArrayList<Fragment>();
    private int currentTabIndex;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        super.initView();
        fragments.add(FootPrintFragment.getInstance("圈子"));
        fragments.add(MapFragment.getInstance("位置"));
        fragments.add(UserFragment.Companion.getInstance("我的"));
        showFragment(fragments.get(1));
    }

    public void onClick(View v) {
        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
        trx.hide(fragments.get(0));
        trx.hide(fragments.get(1));
        trx.hide(fragments.get(2));
        initBackground();
        switch (v.getId()) {
            case R.id.img_footprint:
                if (!fragments.get(0).isAdded()) {
                    trx.add(R.id.content, fragments.get(0));
                }
                trx.show(fragments.get(0)).commit();
                setBackground(imgFootprint);
                break;
            case R.id.img_city:
                if (!fragments.get(1).isAdded()) {
                    trx.add(R.id.content, fragments.get(1));
                }
                trx.show(fragments.get(1)).commit();
                setBackground(imgCity);
                break;
            case R.id.img_user:
                if (!fragments.get(2).isAdded()) {
                    trx.add(R.id.content, fragments.get(2));
                }
                trx.show(fragments.get(2)).commit();
                setBackground(imgUser);
                break;
            default:

                break;

        }
    }

    private void initBackground() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            imgFootprint.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.text_cancel)));
            imgCity.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.text_cancel)));
            imgUser.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.text_cancel)));
        }
    }

    private void setBackground(ImageView img) {

        img.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(this, BaseAppAplication.theme)));
    }

    /**
     * 点两次退出按钮退出
     */
    boolean isExit = false;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(getApplicationContext(), "再按一次退出",
                        Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        isExit = false;
                    }
                }, 2000);
                return false;
            }
        }

        return super.onKeyDown(keyCode, event);


    }


    private void showFragment(Fragment fragment) {

        getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (BaseAppAplication.theme==R.color.black){
            QMUIStatusBarHelper.setStatusBarLightMode(this);
        }else {
            QMUIStatusBarHelper.setStatusBarDarkMode(this);
        }
        setBackground(imgUser);

    }
}
